package br.com.studyLife.interceptor;

import br.com.caelum.vraptor.InterceptionException;
import br.com.caelum.vraptor.Intercepts;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.core.InterceptorStack;
import br.com.caelum.vraptor.interceptor.Interceptor;
import br.com.caelum.vraptor.resource.ResourceMethod;
import br.com.studyLife.controller.HomeController;
import br.com.studyLife.modelo.UsuarioWeb;

@Intercepts
public class AutorizacaoInterceptor implements Interceptor {

	private final UsuarioWeb usuarioWeb;
	private final Result result;
	
	public AutorizacaoInterceptor(UsuarioWeb usuarioWeb, Result result) {
		this.usuarioWeb = usuarioWeb;
		this.result = result;
	}
	
	@Override
	public boolean accepts(ResourceMethod method) {
		return !usuarioWeb.isLogado() && !method.containsAnnotation(Publico.class);
	}

	@Override
	public void intercept(InterceptorStack stack, ResourceMethod method,
			Object resourceInstance) throws InterceptionException {
		result.redirectTo(HomeController.class).index();
	}

}
